//
//  search_viewController.m
//  SoundPill
//
//  Created by 宋鹏鹏 on 15/4/6.
//  Copyright (c) 2015年 宋旭东. All rights reserved.
//

#import "search_viewController.h"
#import "UIViewExt.h"
#import "plaza_viewModel.h"
#import "AFNetworking.h"
#import "JSONKit.h"
#import "MJRefresh.h"
#import "search_worksCell.h"
#import "search_userNameCell.h"
#import "search_labelCell.h"
#import "userInformationViewController.h"
#import "search_labelController.h"
#import "Player_ShowModel.h"
#import "UIViewController+MMDrawerController.h"
#import "MobClick.h"
#import "JCAlertView.h"

@interface search_viewController ()<UISearchBarDelegate,MJRefreshBaseViewDelegate,searchHeadPushDelegate>
{
    UISearchBar *_searchbar;
    //
    UIButton *_lastSelect;
    
    UIButton *searchBtn;
    
    int _page; // 页码
    int _lastpage;
    MJRefreshFooterView *_footer;
    MJRefreshHeaderView *_header;
    
    search_worksCell *_workslistCell;
    search_userNameCell *_userNameCell;
    search_labelCell  *_signCell;
    
    //请求类型
    NSString *_type;
    NSString *_string;
    
    UITableViewCell *cell;
    
    UILabel *label;

}
- (void)loadSegement;
//
//- (void)requestSearchData;

- (void)refreshUI;
@property (strong, nonatomic) NSString *playingAudioId;
@property (strong, nonatomic) NSString *searchReason;

@end

@implementation search_viewController
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.hidesBottomBarWhenPushed = YES;
        
    }
    return self;
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:YES];
    
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    [MobClick beginLogPageView:@"搜索"];
    [_searchbar becomeFirstResponder];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:YES];
    [MobClick endLogPageView:@"搜索"];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    _page = 1;
    _lastpage = 1;
//    self.navigationController.navigationBar.translucent = NO;
    _type = @"作品";
    self.view.backgroundColor = [UIColor whiteColor];
    UIView *lightGrayView = [[UIView alloc]initWithFrame:CGRectMake(self.view.width*0.01, 30, self.view.width*0.75, 25)];
//    lightGrayView.backgroundColor = [UIColor redColor];
    lightGrayView.layer.cornerRadius = 7;
    lightGrayView.layer.masksToBounds = YES;
    
    lightGrayView.layer.borderWidth = 0;
    
//    [self.view addSubview:lightGrayView];
    UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:lightGrayView];
    self.navigationItem.leftBarButtonItem = buttonItem;
    
    _searchbar = [[UISearchBar alloc]initWithFrame:CGRectMake(-6, 0, self.view.width*0.85+12, 25)];
    [[[[ _searchbar . subviews objectAtIndex : 0 ] subviews ] objectAtIndex : 0 ] removeFromSuperview ];
    _searchbar.delegate = self;
    _searchbar.placeholder = @"搜索作品";
    [lightGrayView addSubview:_searchbar];
    
    searchBtn = [[UIButton alloc]initWithFrame:CGRectMake(lightGrayView.right+2, lightGrayView.top-5, 35, 35)];
    searchBtn.titleLabel.font = [UIFont systemFontOfSize:15];
//
    [searchBtn setTitle:@"取消" forState:UIControlStateNormal];
    searchBtn.backgroundColor = [UIColor clearColor];
    [searchBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [searchBtn addTarget:self action:@selector(popViewController) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *searchItem = [[UIBarButtonItem alloc] initWithCustomView:searchBtn];
    self.navigationItem.rightBarButtonItem = searchItem;

    [self loadSegement];
    _worksBtn.selected = YES;
    _lastSelect = _worksBtn;

    //加载表
    _listTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, _blueArticle.bottom + 2, self.view.width, self.view.height-_blueArticle.bottom - 64)];
     _listTableView.separatorStyle = NO;
    _listTableView.dataSource = self;
    _listTableView.delegate = self;
    [self.view addSubview:_listTableView];
    
    //
    listActivityIndicator = [[UIActivityIndicatorView alloc]
                             initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    listActivityIndicator.frame = CGRectMake(self.view.width/2-20, (self.view.height-108)/2, 40, 40);
    
    [self.view addSubview:listActivityIndicator];

    [self addRefresh];
    self.navigationItem.backBarButtonItem = nil;
    if (_searchReason != NULL || _searchReason.length > 0) {
        _searchbar.text = _searchReason;
        [self labelBtnClick];
    }
    
}
- (void)getReason:(NSString *)reason
{
   _searchReason = reason;
}
- (void)backClick
{
    [self performSelector:@selector(popViewController) withObject:nil afterDelay:0.2];
}
- (void)popViewController
{
  [self.navigationController popViewControllerAnimated:YES];
}

- (void)searchClick
{

    
    
    //[listActivityIndicator startAnimating];
    
    //[self requestSearchData];
    if (_searchbar.text.length >0) {
        [_searchbar resignFirstResponder];
        
        NSString *type = @"";
        if (_lastSelect == _worksBtn) {
            type = @"work";
        }else if (_lastSelect == _userNameBtn){
        type = @"user";
        
        }else if(_lastSelect == _labelBtn)
        {
        type = @"tag";
        }
        
        NSString *str2 = [_searchbar.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [self requestData:str2 type:type];
    }
    

}

- (void)loadSegement
{
    
    //添加控制button
    // 作品
    _worksBtn = [[search_segeItem alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    [_worksBtn setTitle:@"作品" forState:UIControlStateNormal];
    [_worksBtn setTitle:@"作品" forState:UIControlStateSelected];
    //
    [_worksBtn addTarget:self action:@selector(worksBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_worksBtn];
    
    //用户
    _userNameBtn = [[search_segeItem alloc]initWithFrame:CGRectMake(_worksBtn.right, _worksBtn.top, 0, 0)];
    [_userNameBtn setTitle:@"用户" forState:UIControlStateNormal];
    [_userNameBtn setTitle:@"用户" forState:UIControlStateSelected];
    //
    [_userNameBtn addTarget:self action:@selector(userNameBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_userNameBtn];
    
    
    //标签
    _labelBtn = [[search_seglastItem alloc]initWithFrame:CGRectMake(_userNameBtn.right, _worksBtn.top, 0, 0)];
    [_labelBtn setTitle:@"标签" forState:UIControlStateNormal];
    [_labelBtn setTitle:@"标签" forState:UIControlStateSelected];
    //
    [_labelBtn addTarget:self action:@selector(labelBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_labelBtn];
    
    
    //添加蓝色选中条
    _blueArticle = [[UIImageView alloc]initWithFrame:CGRectMake(0 , _worksBtn.bottom-3.5, _worksBtn.width, 3)];
    _blueArticle.image = [UIImage imageNamed:@"select_line"];
    [self.view addSubview:_blueArticle];
    
    
}
//点击作品
- (void)worksBtnClick
{
   // [listActivityIndicator startAnimating];

    
    _lastSelect.selected=NO;
    _worksBtn.selected = YES;
    _lastSelect = _worksBtn;
    
    _searchbar.placeholder = @"搜索作品";
    [UIView animateWithDuration:0.3 animations:^{
        _blueArticle.frame = CGRectMake(0 , _worksBtn.bottom-3.5, _worksBtn.width, 3);
        
    }];
    
    _type = @"作品";
    //[self requestSearchData];
    if (_searchbar.text.length>0) {
        
        NSString *str2 = [_searchbar.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [self requestData:str2 type:@"work"];
        
        [_listTableView performSelector:@selector(reloadData) withObject:nil afterDelay:1];
    }
    
    
    
}

//点击用户
- (void)userNameBtnClick
{
    //[listActivityIndicator startAnimating];
    
    _type = @"用户";
    if (_searchbar.text.length>0) {
        
        NSString *str2 = [_searchbar.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [self requestData:str2 type:@"user"];
    }

     _lastSelect.selected=NO;
    _userNameBtn.selected = YES;
    _lastSelect = _userNameBtn;
    _searchbar.placeholder = @"搜索用户";
    [UIView animateWithDuration:0.3 animations:^{
        _blueArticle.frame = CGRectMake(_worksBtn.width  , _worksBtn.bottom-3.5, _worksBtn.width, 3);
    }];
    
   
    //[self requestSearchData];
    
    
    [_listTableView performSelector:@selector(reloadData) withObject:nil afterDelay:1];
    
}

//点击标签
- (void)labelBtnClick
{
    //[listActivityIndicator startAnimating];
    
    _lastSelect.selected=NO;
    _labelBtn.selected = YES;
    _lastSelect = _labelBtn;
    _searchbar.placeholder = @"搜索标签";
    [UIView animateWithDuration:0.3 animations:^{
        _blueArticle.frame = CGRectMake(_worksBtn.width * 2 , _worksBtn.bottom-3.5, _worksBtn.width, 3);
    }];
    
    _type = @"标签";
    //[self requestSearchData];
    if (_searchbar.text.length>0) {
        
        NSString *str2 = [_searchbar.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [self requestData:str2 type:@"tag"];
    }

     [_listTableView performSelector:@selector(reloadData) withObject:nil afterDelay:1];
    
    
}
//
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [_searchbar resignFirstResponder];
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    if (searchText!= nil) {
       // searchBtn.alpha = 1;
        
    }else
    {
    //searchBtn.alpha = 0.3;
    }

}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
    
    //[self requestSearchData];
    [self searchClick];
    
    
}




- (void)requestData:(NSString *)string type:(NSString *)type
{
//    NSLog(@"--kkll--%@",type);
    
    _string = string;
    
    _searchArray = [NSMutableArray array];
    
     AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    //    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
    NSDictionary *data = [[NSDictionary alloc]init];

        NSString *fansString=[NSString stringWithFormat:@"http://123.57.1.143/api/%@/search?q=%@",type,string];
        [manager GET:fansString parameters:data progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSDictionary *dict= responseObject; //[NSJSONSerialization  JSONObjectWithData:data options:0 error:nil];
            // NSLog(@"获取到的数据为：%@",dict);
            
            self.tempArray22 = [dict objectForKey:@"data"];
            
            // 1.添加数据
            NSMutableArray *arrM = [[NSMutableArray alloc] init];
            [arrM addObjectsFromArray:_searchArray];
            [arrM addObjectsFromArray:self.tempArray22];
            _searchArray = arrM;
            
            [self performSelector:@selector(successfulled) withObject:nil afterDelay:0.3];
            
            [self performSelector:@selector(hidevicator) withObject:nil afterDelay:0.5];
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            _listTableView.hidden = YES;
            
            listActivityIndicator.hidden = NO;
            
            [self performSelector:@selector(hidevicator) withObject:nil afterDelay:1];
        }];
}
- (void)hidevicator
{
    [listActivityIndicator stopAnimating];
    listActivityIndicator.hidden = YES;
    [listActivityIndicator removeFromSuperview];
}
//
- (void)successfulled
{
    [label removeFromSuperview];
    label = nil;
    
    if (_searchArray.count<1) {
        
       //未找到关于“XX”的作品/用户/标签
        NSString *string = [NSString stringWithFormat:@"糟糕！还没有关于“%@”的%@",_searchbar.text,_type];
        label = [[UILabel alloc]initWithFrame:CGRectMake(10, 70+55, self.view.width-20, 30)];
        label.text = string;
        label.textColor = [UIColor lightGrayColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:15];
        label.backgroundColor = [UIColor clearColor];
        [self.view addSubview:label];
        
        
    }
    
    //
    _listArray = [[NSMutableArray alloc] initWithCapacity:_searchArray.count];
    
    for (int i=0; i<[_searchArray count]; i++) {
        NSDictionary *item=[_searchArray objectAtIndex:i];
        // NSString *BrandName=[item objectForKey:@"BrandName"];
        
        plaza_viewModel *foucsModel = [[plaza_viewModel alloc] init];
        
        foucsModel.type = [item objectForKey:@"type"];
        foucsModel.title =[item objectForKey:@"title"];
                foucsModel.UserID = [item objectForKey:@"uid"];
        foucsModel.CinemeID = [item objectForKey:@"id"];
        foucsModel.releaseDate = [item objectForKey:@"created_at"];
        foucsModel.headImageURL = [item objectForKey:@"avatar"];
        if ( _lastSelect == _worksBtn) {
            foucsModel.personName = [[item objectForKey:@"user"] valueForKey:@"username"];
        } else {
            foucsModel.personName = [item objectForKey:@"username"];
        }
        foucsModel.worksImageURL = [item objectForKey:@"cover"];
        foucsModel.tagCount = [item objectForKey:@"count"];
        foucsModel.tagName = [item objectForKey:@"name"];
        foucsModel.playTimes = [item objectForKey:@"play_count"];
        foucsModel.heartCount = [item objectForKey:@"love_count"];
        //是否关注========
        //foucsModel.isEachFoucs = [item objectForKey:@"following"];
        
        
        [_listArray addObject:foucsModel];
        
        
    }
    
    [self performSelector:@selector(refreshUI) withObject:nil afterDelay:0.09];
    
    
}


//
- (void)refreshUI
{

    _listTableView.hidden = NO;
    
    [_listTableView reloadData];


}

#pragma mark 添加刷新控件
- (void)addRefresh
{
    MJRefreshHeaderView *header = [MJRefreshHeaderView header];
//    header.scrollView = _listTableView;
    header.delegate = self;
    _header = header;
    
    MJRefreshFooterView *footer = [MJRefreshFooterView footer];
    footer.scrollView = _listTableView;
    footer.delegate = self;
    _footer = footer;
    _footer.hidden = YES;
}

#pragma mark - 刷新代理方法
- (void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView
{
    BOOL isHeader = [refreshView isKindOfClass:[MJRefreshHeaderView class]];
    if (isHeader) { // 下拉刷新
        // 清除图片缓存
        //[TGImageTool clear];
        _page = 1; // 第一页
        
        _lastpage = _page;
        
    } else { // 上拉加载更多
        
        _page++;
        _lastpage = _page;
        
    }
    if (_string.length > 0 && _listArray.count > 6) {
        _footer.hidden = NO;
    
    
//    NSString *page = [NSString stringWithFormat:@"%i",_page];
    NSString *myString=[NSString stringWithFormat:@"http://123.57.1.143/api/%@/search?q=%@",_type,_string];
    
    NSString *url = [myString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
     AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        _tempArray = [responseObject objectForKey:@"data"];
        //NSLog(@"--------,,%@",_tempArray);
        if (_tempArray.count > 0) {
            
            
            [refreshView performSelector:@selector(endRefreshing) withObject:nil afterDelay:0.9];
            
            [self performSelector:@selector(loadNextPage) withObject:nil afterDelay:0.5];
            
        }else {
            
            [refreshView endRefreshing];
            _lastpage --;
            if (_lastpage>0) {
                _page = _lastpage;
            }
            
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        _lastpage --;
        if (_lastpage>0) {
            _page = _lastpage;
        }
        
        //提示用户加载失败
        [refreshView endRefreshing];
    }];
    
   }else{
      _footer.hidden = YES;
   }
}

- (void)loadNextPage
{
    if ((_tempArray.count > 0 &&_searchArray.count < 1 )||(_tempArray.count > 0 && _page>1)) {
        // 1.添加数据
        NSMutableArray *arrM = [[NSMutableArray alloc] init];
        [arrM addObjectsFromArray:_searchArray];
        [arrM addObjectsFromArray:_tempArray];
        _searchArray = arrM;
    }
    
    // NSLog(@"____%d",self.songArray.count);
    
    [self successfulled];
    
    [_listTableView reloadData];
    
    // 3.恢复刷新状态
    
    // 4.根据数量判断是否需要隐藏上拉控件
    // _footer.hidden = _deals.count >= totalCount;
    
    
    
    
}



#pragma mark - TableView DataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_lastSelect == _worksBtn) {
        return self.view.frame.size.height / 7;
    } else if (_lastSelect == _userNameBtn) {
       return ([UIScreen mainScreen].bounds.size.height - 64) / 10;
    } else {
        return 65;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_listArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
      if (_lastSelect==_worksBtn) {
    
    // 定义静态标识符
    //static NSString *cellIdentifier = @"cell";
    NSString *cellIdentifier = @"worklineCell";
    
    // 检查表视图中是否存在闲置的单元格
    _workslistCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (_workslistCell == nil) {
        
        _workslistCell = [[search_worksCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    _workslistCell.plazadataModel = _listArray[indexPath.row];
    
          cell = _workslistCell;
          
        }else if (_lastSelect == _userNameBtn){
    
            // 定义静态标识符
            //static NSString *cellIdentifier = @"cell";
            NSString *cellIdentifier = @"usernameCell";
    
            // 检查表视图中是否存在闲置的单元格
            _userNameCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
            if (_userNameCell == nil) {
    
                _userNameCell = [[search_userNameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
            }
    
            _userNameCell.plazadataModel = _listArray[indexPath.row];
    
            _userNameCell.searchPushDelegate = self;
    
          cell = _userNameCell;
    
        }else if(_lastSelect == _labelBtn){
        
            NSString *cellIdentifier = @"labelCell";
            
            // 检查表视图中是否存在闲置的单元格
            _signCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
            
            if (_signCell == nil) {
                
                _signCell = [[search_labelCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
            }
            
            _signCell.plazadataModel = _listArray[indexPath.row];
            
            
            
            cell = _signCell;

        
        }
    
    
    
    return cell;
    
}

 //代理实现
- (void)searchHeadPush:(NSString *)userId
{
    NSUserDefaults *userDefaultes = [NSUserDefaults standardUserDefaults];
    //读取字典类型NSDictionary类型的数据
    NSString *uid = [userDefaultes stringForKey:@"uid"];
    
    if (![userId isEqualToString: uid] ) {
        
        userInformationViewController *otherSelfVC = [[userInformationViewController alloc]initWithSelfUid:userId];
        [self.navigationController pushViewController:otherSelfVC animated:YES];
        
    }else if([userId isEqualToString: uid]){
        
        //自动跳到我的
        if (self.tabBarController.selectedIndex == 4) {
            [self.navigationController popToRootViewControllerAnimated:YES];
        } else {
            [self.tabBarController setSelectedIndex:4];
            
        }
        
    }

}

#pragma -mark 点击标签cell的跳转
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_lastSelect==_labelBtn) {
        
        //存label的model数据
//        NSString *string = [_listArray[indexPath.row] objectForKey:@"tagName"];
//        NSLog(@"tagString--%@",string);
        plaza_viewModel *model = _listArray[indexPath.row];
    search_labelController *searchLabelVC = [[search_labelController alloc]init];
        
        [searchLabelVC getTitle:model.tagName];
    [self.navigationController pushViewController:searchLabelVC animated:YES];
        
    }else if(_lastSelect==_worksBtn){
        
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        NSString *isPlay = [userDefaults objectForKey:@"isWiFiPlay"];
        
        NSString *pppp= [NSString stringWithFormat:@"%u",[self getNetworkTypeFromStatusBar]];
        if (([isPlay isEqualToString:@"1"]&&[pppp isEqualToString:@"5"])||[pppp isEqualToString:@"5"]||([isPlay isEqualToString:@"0"]&&![pppp isEqualToString:@"0"])||isPlay==nil) {
        //播放功能
        UINavigationController *nv = (UINavigationController *)self.mm_drawerController.rightDrawerViewController;
            self.player = (playerTableViewController *)nv.topViewController;
            
        Player_ShowModel *playModel = [Player_ShowModel sharedInstance];
        plaza_viewModel *model = _listArray[indexPath.row];
        
        NSString *showURL = [NSString stringWithFormat:@"http://123.57.1.143/api/work/%@/show", model.CinemeID];
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.responseSerializer = [AFJSONResponseSerializer serializer];
        NSString *header = [NSString stringWithFormat:@"Token %@:%@",[[NSUserDefaults standardUserDefaults] valueForKey:@"uid"], [[NSUserDefaults standardUserDefaults] valueForKey:@"token"]];
        [manager.requestSerializer setValue:header forHTTPHeaderField:@"authorization"];
            [manager GET:showURL parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                NSDictionary *dic = (NSDictionary *)responseObject;
                NSDictionary *dataDic = [dic valueForKey:@"data"];
                NSDictionary *linksDic = [dic valueForKey:@"links"];
                //            NSLog(@"00000---%@", dataDic);
                if ([[dic allKeys] containsObject:@"meta"]) {
                    NSDictionary *meta = [dic valueForKey:@"meta"];
                    NSString *userAvatar = [meta valueForKey:@"userAvatar"];
                    _player.locUserAvatar = userAvatar;
//                    NSArray *array = [meta valueForKey:@"playlist"];
//                    _player.playList = [NSMutableArray arrayWithArray:array]
                    ;
                    playModel.comment_count = [meta valueForKey:@"count"];
                    ;
                }
                
                [playModel setValuesForKeysWithDictionary:dataDic];
                _player.waveform = [linksDic valueForKey:@"waveform"];
                _player.workID = model.CinemeID;
                _player.isSelf = NO;
                //保存是否love
                NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
                NSString *islove = @"0";
                if ([[dataDic objectForKey:@"liked"]boolValue]==YES) {
                    islove = @"1";
                }else{
                    islove = @"0";
                }
                [userDefaults setObject:islove forKey:@"isLove"];
                [userDefaults synchronize];
                
                [_player reloadViews:playModel];
                [self.mm_drawerController toggleDrawerSide:MMDrawerSideRight animated:YES completion:^(BOOL finished) {
                }];
                
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                
            }];
        }else{
            [JCAlertView showOneButtonWithTitle:@"你已开启仅Wifi环境播放" Message:@"可以去设置关闭此功能" ButtonType:JCAlertViewButtonTypeDefault ButtonTitle:@"确定" Click:^{
                
            }];
        }
    }

}
//
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    
    if (alertView.tag==80002) {
        if (buttonIndex == 0) {
            
        }else if (buttonIndex == 1){
            
            //跳到设置
            set_ViewController *setVC = [[set_ViewController alloc]init];
            [self.navigationController pushViewController:setVC animated:YES];
            
        }
        
    }
}

-(NETWORK_state)getNetworkTypeFromStatusBar {
    UIApplication *app = [UIApplication sharedApplication];
    NSArray *subviews = [[[app valueForKey:@"statusBar"] valueForKey:@"foregroundView"] subviews];
    NSNumber *dataNetworkItemView = nil;
    for (id subview in subviews) {
        if([subview isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]])     {
            dataNetworkItemView = subview;
            break;
        }
    }
    NETWORK_state nettype = NETWORK_state_NONE;
    NSNumber * num = [dataNetworkItemView valueForKey:@"dataNetworkType"];
    nettype = [num intValue];
    return nettype;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
